﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Antu.Business.Business;
using Antu.Business;
using Antu.Business.AbstractClass;

namespace DataTranslation
{
    /// <!--
    /// 功能描述  : 物业申请信息交换
    /// 创建人  : LinJian
    /// 创建时间: 2007-07-09
    /// -->
    public partial class FrmDataTranslation : Form
    {
        private AbstractDataOperation clsDataTranlation;

        public FrmDataTranslation()
        {
            InitializeComponent();

            clsDataTranlation = DataTranslationFactory.CreateDataOperation(
                GlobalSetting.SystemTranslationType);
            SetControlCaption();
        }

        #region 设置控件名称
        /// <summary>
        /// 设置控件名称
        /// </summary>
        private void SetControlCaption()
        {
            if (GlobalSetting.SystemTranslationType == OperationType.WuYeRegisterExchange)
            {
                this.Text = "物业企业注册信息交换";
            }
            if (GlobalSetting.SystemTranslationType == OperationType.WuYeRequestExchange)
            {
                this.Text = "物业企业申请信息交换";
            }
            if (GlobalSetting.SystemTranslationType == OperationType.ChaiQianSysExchange)
            {
                this.Text = "城市拆迁项目申请信息交换";
            }
            if (GlobalSetting.SystemTranslationType == OperationType.TaskDataExchange)
            {
                this.Text = "业务办理信息交换";
            }
        } 
        #endregion

        #region 获取临时数据存储的类型
        /// <summary>
        /// 获取临时数据存储的类型
        /// </summary>
        private void GetTempDatabaseType()
        {
            if (rbtnFileMdb.Checked)
            {
                GlobalSetting.TempDatabaseFileType = TempDataType.Mdb;
            }
            else if (rbtnFileXml.Checked)
            {
                GlobalSetting.TempDatabaseFileType = TempDataType.Xml;
            }
        } 
        #endregion

        /********** 导出动作 ************/

        #region 导出外网数据到临时文件
        /// <summary>
        /// 导出外网数据到临时文件
        /// </summary>
        private void ExportWuYeRequestData()
        {
            
            DataSet dsGet = clsDataTranlation.WebToTempFile(DataTranslationFactory.ExportTables);

            if (dsGet.Tables.Count == 0)
            {
                ClsMsgBoxAppear.MsgBoxError("没有任何数据可以导出");
                return;
            }

            if (GlobalSetting.TempDatabaseFileType == TempDataType.Xml)
            {
                SaveFileDialog ofdDialog = new SaveFileDialog();
                ofdDialog.InitialDirectory = Application.StartupPath;
                ofdDialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
                if (ofdDialog.ShowDialog() == DialogResult.OK)
                {
                    //获取文件名
                    string strFileName = ofdDialog.FileName;

                    //导出xml
                    dsGet.WriteXml(strFileName);
                }
            }
            else if (GlobalSetting.TempDatabaseFileType == TempDataType.Mdb)
            {
                //导出xml
                clsDataTranlation.ExportData(dsGet);
            }

            MessageBox.Show("导出成功");
        }

        /// <summary>
        /// 外网数据导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, EventArgs e)
        {
            Application.DoEvents();
            this.Cursor = Cursors.WaitCursor;

            try
            {                
                //获取导入的数据类型
                GetTempDatabaseType();

                //导出申请信息
                ExportWuYeRequestData();
            }
            catch (Exception ex)
            {
                ClsMsgBoxAppear.MsgBoxError(ex);
            }
            finally
            {
                Application.DoEvents();
                this.Cursor = Cursors.Default;
            }
        } 
        #endregion

        #region 临时文件数据导入到内网
        /// <summary>
        /// 外网临时文件数据导入到内网
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImport_Click(object sender, EventArgs e)
        {
            Application.DoEvents();
            this.Cursor = Cursors.WaitCursor;

            try
            {
                //获取导入的数据类型
                GetTempDatabaseType();

                //获取选择表的数据
                DataSet dsGet = new DataSet();

                //弹出文件选择框
                if (GlobalSetting.TempDatabaseFileType == TempDataType.Xml)
                {
                    OpenFileDialog ofdDialog = new OpenFileDialog();
                    ofdDialog.InitialDirectory = Application.StartupPath;
                    ofdDialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
                    if (ofdDialog.ShowDialog() == DialogResult.OK)
                    {
                        //获取文件名
                        string strFileName = ofdDialog.FileName;

                        //获取xml数据
                        dsGet.ReadXml(strFileName);
                    }
                    else
                    {
                        return;
                    }
                }
                else if (GlobalSetting.TempDatabaseFileType == TempDataType.Mdb)
                {
                    //获取mdb数据
                }

                if (dsGet.Tables.Count > 0)
                {
                    //执行导入        
                    clsDataTranlation.TempFileToInside(DataTranslationFactory.ExportTables, dsGet);

                    MessageBox.Show("导入成功");
                }
                else
                {
                    MessageBox.Show("导入的数据文件是空文件，请查看");
                }
            }
            catch (Exception ex)
            {
                ClsMsgBoxAppear.MsgBoxError(ex);
            }
            finally
            {
                Application.DoEvents();
                this.Cursor = Cursors.Default;
            }
        } 
        #endregion

        #region 内网审批数据导出临时文件
        /// <summary>
        /// 内网审批数据导出临时文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExportFromInside_Click(object sender, EventArgs e)
        {
            Application.DoEvents();
            this.Cursor = Cursors.WaitCursor;

            try
            {
                //获取导入的数据类型
                GetTempDatabaseType();

                if (GlobalSetting.TempDatabaseFileType == TempDataType.Xml)
                {
                    SaveFileDialog ofdDialog = new SaveFileDialog();
                    ofdDialog.InitialDirectory = Application.StartupPath;
                    ofdDialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
                    if (ofdDialog.ShowDialog() == DialogResult.OK)
                    {
                        //获取文件名
                        string strFileName = ofdDialog.FileName;

                        //导出xml
                        DataSet dsGet = clsDataTranlation.InsideToTempFile(DataTranslationFactory.ExportTables);

                        if (dsGet.Tables.Count > 0)
                        {
                            dsGet.WriteXml(strFileName);
                            MessageBox.Show("导出成功");
                        }
                        else
                        {
                            ClsMsgBoxAppear.MsgBoxError("数据库中无任何数据可以导出");
                        }
                    }
                }
                else if (GlobalSetting.TempDatabaseFileType == TempDataType.Mdb)
                {
                }
            }
            catch (Exception ex)
            {
                ClsMsgBoxAppear.MsgBoxError(ex);
            }
            finally
            {
                Application.DoEvents();
                this.Cursor = Cursors.Default;
            }
        } 
        #endregion

        #region 临时文件数据导出到外网
        /// <summary>
        /// 临时文件数据导出到外网
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImportFromWeb_Click(object sender, EventArgs e)
        {
            Application.DoEvents();
            this.Cursor = Cursors.WaitCursor;

            try
            {
                //获取导入的数据类型
                GetTempDatabaseType();

                //获取选择表的数据
                DataSet dsGet = new DataSet();

                //弹出文件选择框
                if (GlobalSetting.TempDatabaseFileType == TempDataType.Xml)
                {
                    OpenFileDialog ofdDialog = new OpenFileDialog();
                    ofdDialog.InitialDirectory = Application.StartupPath;
                    ofdDialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
                    if (ofdDialog.ShowDialog() == DialogResult.OK)
                    {
                        //获取文件名
                        string strFileName = ofdDialog.FileName;

                        //获取xml数据
                        dsGet.ReadXml(strFileName);
                    }
                    else
                    {
                        return;
                    }
                }
                else if (GlobalSetting.TempDatabaseFileType == TempDataType.Mdb)
                {
                    //获取mdb数据
                }

                if (dsGet.Tables.Count == 0)
                {
                    ClsMsgBoxAppear.MsgBoxError("数据文件为空，没有任何数据可以导入");
                    return;
                }
                else
                {
                    //执行导入
                    clsDataTranlation.TempFileToWeb(DataTranslationFactory.ExportTables, dsGet);
                    MessageBox.Show("导入成功");
                }
            }
            catch (Exception ex)
            {
                ClsMsgBoxAppear.MsgBoxError(ex);
            }
            finally
            {
                Application.DoEvents();
                this.Cursor = Cursors.Default;
            }
        } 
        #endregion
    }
}